/**
 * TabVD is a Virtual Desktop Client/Server designed specifically for connecting
 * from Tablet like devices.
 *
 * Copyright (C) 2011 Subhabrata Ghosh
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * AbstractLogger.cpp
 *
 *  Created on: May 26, 2011
 *      Author: subhagho
 */

#include "AbstractLogger.h"

namespace tabvd {

    AbstractLogger::AbstractLogger() {
        isrunning = false;
        loglevel = LOGLEVEL_ERROR;
    }

    AbstractLogger::~AbstractLogger() {

    }

    void AbstractLogger::setlevel(ELevel level) {
        loglevel = level;
    }

    std::string* AbstractLogger::getmessage(ELevel level, const char* format, va_list args) {
        std::string* mesgstr = new std::string();
        if (mesgstr) {
            DateTime* dt = new DateTime();
            if (dt) {
                mesgstr->append(*(dt->toString())).append(" : ");
                switch(level) {
LOGLEVEL_ERROR:
                    mesgstr->append("ERROR : ");
                    break;
LOGLEVEL_WARNING:
                    mesgstr->append("WARNING : ");
                    break;
LOGLEVEL_DEBUG:
                    mesgstr->append("DEBUG : ");
                    break;
LOGLEVEL_PRINT:
                    mesgstr->append("INFO : ");
                }
            }
            char buffer[_MAX_MESG_BUFFER_SZ_];
            memset(buffer, 0, _MAX_MESG_BUFFER_SZ_);

            vsnprintf(buffer, _MAX_MESG_BUFFER_SZ_, format, args);
            mesgstr->append(buffer);
        }
        mesgstr->append("\n");
        return mesgstr;

    }

    bool AbstractLogger::shouldlog(ELevel level) {
        if (!isrunning)
            return false;

        if (level <= loglevel)
            return true;
        return false;
    }
}
